package com.bdqn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.pojo.Right;
import com.bdqn.pojo.Role;
import com.bdqn.service.RoleService;
import com.bdqn.shiro.MyShiroRealm;
import jakarta.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 2024-10-28
 * @Author:T369曹任柱
 */
@Controller
@RequestMapping(value = "role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @RequestMapping(value = "/list")
    public String list(@RequestParam(required = false,defaultValue = "")String roleName, @RequestParam(required = false,defaultValue = "1")int PageIndex, Model model){
        Sort sort=new Sort(Sort.Direction.ASC,"roleId");
        Pageable pageable= PageRequest.of(PageIndex-1,3,sort);
        Page<Role> rolePage=roleService.findRole(roleName,pageable);
        model.addAttribute("rolePage",rolePage);
        model.addAttribute("roleName",roleName);
        return "role/list";
    }

    @RequestMapping(value = "/add")
    public String add(Model model){
        List<Right> rights=roleService.findAllRights();
        model.addAttribute("rights",rights);
        return "role/add";
    }
    @RequestMapping(value = "/edit")
    public String edit(Long roleId,Model model){
        Role role=roleService.getRole(roleId);
        model.addAttribute("role",role);
        List<Right> roleRights=roleService.findRightsByRole(role);
        model.addAttribute("roleRights",roleRights);
        List<Right> rights=roleService.findAllRights();
        model.addAttribute("rights",rights);
        return "role/edit";
    }
    @RequestMapping(value ="save")
    public String save(Role role,String[] rightCodes){
        if(rightCodes!=null){
            for (String rightCode:rightCodes){
                Right right=new Right();
                right.setRightCode(rightCode);
                role.getRights().add(right);
            }
        }
        roleService.saveRole(role);
        RealmSecurityManager rsm= (RealmSecurityManager) SecurityUtils.getSecurityManager();
        MyShiroRealm realm= (MyShiroRealm) rsm.getRealms().iterator().next();
        realm.cleatAllCachedAuthorizationInfo();
        return "redirect:/role/list";
    }
    @RequestMapping(value = "/del")
    @ResponseBody
    public Map del(Long roleId){
        Map map=new HashMap();
        try {
            roleService.deleteRoleById(roleId);
            map.put("delResult",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("delResult",false);
        }
        return map;
    }

}
